const ScoreModel = require('../models/score')

class Score {

  // 成绩修改
  async update(ctx) {
    const { user } = ctx.state
    if (user.role == 1) {
      const {  scoreId,score } = ctx.request.body
      const res = await ScoreModel.updateOne({ _id:scoreId }, { score })
      if (res) {
        return ctx.send('成绩修改成功')
      } else {
        return ctx.sendError('000002', '成绩修改失败')
      }
    } else{
      return ctx.sendError('000002', '没有权限')
    }
  }
  // 成绩查询
  async inquire(ctx) {
    const { user } = ctx.state
      let { pageNum = 1, pageSize = 10,course,student } = ctx.request.body
      pageNum--
      pageNum = parseInt(pageNum)
      pageSize = parseInt(pageSize)
      let filter={
        $or:[{
          course
        }]
      }
      if(student){
        filter={
          $or:[{
            course,
            student
          }]
        }
      }
      const maxNum = await ScoreModel.find(filter).countDocuments(true)
        await ScoreModel.find(filter)
          .populate({
            path: 'student',
            select: '_id number username'
          })
          .skip(pageNum * pageSize)
          .limit(pageSize)
          .then(data => {
            return ctx.send({ scoreLists: data, totalPage: maxNum })
          })
          .catch(err => {
            return ctx.sendError('000002')
          })

  }
}

module.exports = new Score()
